package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.seller.trading.api.parameter.GetOrderFundInfoBean;
import com.ymatou.query.seller.trading.api.parameter.GetOrderFundInfoResponse;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.common.entitie.C2COrderInfo;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;

/**
 * 获取订单资金信息
 * 接口已经转接java
 */
@TestSuite
public class Ts_GetOrderFundInfo {
	private static GetOrderFundInfoBean getOrderFundInfoBean;
//	private static com.ymatou.query.seller.trading.api.service.GetOrderFundInfoCall getOrderFundInfoCall;
	private static com.ymatou.iapi.trading.sellerquery2.service.GetOrderFundInfoCall getOrderFundInfoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取订单资金信息");
	}

	@Before
	public void caseUp() {
		getOrderFundInfoBean = new GetOrderFundInfoBean();
//		getOrderFundInfoCall = new com.ymatou.query.seller.trading.api.service.GetOrderFundInfoCall();
		getOrderFundInfoCall = new com.ymatou.iapi.trading.sellerquery2.service.GetOrderFundInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getOrderFundInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetOrderFundInfoResponse expect = new GetOrderFundInfoResponse();
		ConstructExpectedResponse(expect);
		GetOrderFundInfoResponse actual = getOrderFundInfoCall
				.getOrderFundInfoResponse();
		DataUtil.verifyEquals(expect.getBuyerLoginId(),
				actual.getBuyerLoginId(), "验证BuyerLoginId");
		DataUtil.verifyDecimalEquals(expect.getBuyerPaidCashAmount(),
				actual.getBuyerPaidCashAmount(), "验证BuyerPaidCashAmount");
		DataUtil.verifyDecimalEquals(expect.getBuyerRefundedCashAmount(),
				actual.getBuyerRefundedCashAmount(),
				"验证BuyerRefundedCashAmount");
		DataUtil.verifyDecimalEquals(expect.getBuyerRefundedGiftAmount(),
				actual.getBuyerRefundedGiftAmount(),
				"验证BuyerRefundedGiftAmount");
		DataUtil.verifyDecimalEquals(expect.getBuyerUsedCouponAmount(),
				actual.getBuyerUsedCouponAmount(), "验证BuyerUsedCouponAmount");
		DataUtil.verifyDecimalEquals(expect.getBuyerUsedGiftAmount(),
				actual.getBuyerUsedGiftAmount(), "验证BuyerUsedGiftAmount");
		DataUtil.verifyEquals(expect.getOrderId(), actual.getOrderId(),
				"验证OrderId");
		DataUtil.verifyEquals(expect.getOrderStatus(), actual.getOrderStatus(),
				"验证OrderStatus");
//		DataUtil.verifyDecimalEquals(expect.getRefundRelatedCouponAmount(),
//				actual.getRefundRelatedCouponAmount(),
//				"验证RefundRelatedCouponAmount");
		DataUtil.verifyDecimalEquals(expect.getRefundRelatedCouponAmount(),
				DataUtil.BigDecimall.GetValueOrDefault(actual.getRefundRelatedCouponAmount()),
				"验证RefundRelatedCouponAmount");
		DataUtil.verifyEquals(expect.getSellerLoginId(),
				actual.getSellerLoginId(), "验证SellerLoginId");
		// DataUtil.verifyEquals(expect.getBuyerId(), actual.getBuyerId(),
		// "验证BuyerId");
		// DataUtil.verifyEquals(expect.getSellerId(), actual.getSellerId(),
		// "验证SellerId");
		// DataUtil.verifyEquals(expect.getBuyerUsedCouponCode(),
		// actual.getBuyerUsedCouponCode(), "验证BuyerUsedCouponCode");
		DataUtil.verifyEquals(0, actual.getBuyerId(), "验证BuyerId");
		DataUtil.verifyEquals(0, actual.getSellerId(), "验证SellerId");
		DataUtil.verifyEquals(null, actual.getBuyerUsedCouponCode(),
				"验证BuyerUsedCouponCode");

	}

	@SuppressWarnings("rawtypes")
	private void ConstructExpectedResponse(GetOrderFundInfoResponse response) {
		List<Map> results = execute(getOrderFundInfoBean.getOrderId());
		ConvertResult(response, results);
	}

	@SuppressWarnings("rawtypes")
	private void ConvertResult(GetOrderFundInfoResponse response,
			List<Map> results) {
		if (DataUtil.isNullOrEmpty(results) == false) {
			Map m = results.get(0);
			DataUtil.mapToBean(response, m);
		}
	}

	@SuppressWarnings("rawtypes")
	private List<Map> execute(Integer orderId) {
		YmtOrdersWapper ymtOrdersWapper = new YmtOrdersWapper();
		StringBuffer queryField = new StringBuffer();
		queryField.append("top 1 o.iorderid as orderId");
		queryField.append(" ,o.iTradingStatus as orderStatus");
		queryField.append(" ,o.iUserId as buyerId");
		queryField.append(" ,o.iBuyerId as sellerId");
		queryField.append(" ,o.sBuyerLoginId as buyerLoginId");
		queryField.append(" ,o.sSellerLoginId as sellerLoginId");
		queryField
				.append(" ,e.fPaidAmountOfCash + e.fPostPaidAmountOfCash as buyerPaidCashAmount");
		queryField
				.append(" ,(case when o.fYmtCouponAmount > 0 then o.fYmtCouponAmount");
		queryField
				.append(" when o.CouponValue > 0 and isnull(o.iCouponChannel, 0) <> 2 then o.CouponValue");
		queryField.append(" else 0 end) as buyerUsedCouponAmount");
		queryField.append(" ,o.sYmtCouponCode as buyerUsedCouponCode");
		queryField
				.append(" ,e.fPaidAmountOfGift + e.fPostPaidAmountOfGift as buyerUsedGiftAmount");
		queryField
				.append(" ,e.fRefundedAmountOfCash as buyerRefundedCashAmount");
		queryField
				.append(" ,e.fRefundedAmountOfGift as buyerRefundedGiftAmount");
		queryField
				.append(" ,(select sum(SettlementAmountOfCoupon) from Ymt_RefundBill(nolock) where OrderId = o.iOrderId and SalesRefundStatus = 10) as refundRelatedCouponAmount");
		String joinCondition = " left join ymt_orderstate(nolock) e on o.iorderid = e.iorderid";
		return ymtOrdersWapper.selectOrders2(queryField.toString(),
				joinCondition, " o.iorderid = " + orderId);
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_001() {
		Logger.start(true, "获取订单资金信息成功,订单状态为1");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create1_BatchC2COrder(userId, sellerId, 1)
					.get(0);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_002() {
		Logger.start(true, "获取订单资金信息成功，订单状态为2");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create2_PayOrder(userId, sellerId);
			Thread.sleep(1000l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_003() {
		Logger.start(true, "获取订单资金信息成功，订单状态为3");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create3_ShippedOrder(userId, sellerId);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_004() {
		Logger.start(true, "获取订单资金信息成功，订单状态为4, 收货");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create4_ReceivedOrder(userId, sellerId);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_005() {
		Logger.start(true, "获取订单资金信息成功，订单状态为4, 申请退款");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create4_ApplyRefundOrder(userId, sellerId);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_006() {
		Logger.start(true, "获取订单资金信息成功，订单状态为4, 退款成功");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create4_RefundOrder(userId, sellerId);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_007() {
		Logger.start(true, "获取订单资金信息成功，订单状态为16");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create16_ApplyForPostPayOrder(userId,
					sellerId);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_008() {
		Logger.start(true, "获取订单资金信息成功，订单状态为12");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create12_UserCancelOrder(userId, sellerId);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_009() {
		Logger.start(true, "获取订单资金信息成功，订单状态为13");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create13_SellerCancelOrder(userId, sellerId);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_010() {
		Logger.start(true, "获取订单资金信息成功，订单状态为18");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create18_SystemCancelOrder(userId, sellerId);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_011() {
		Logger.start(true, "获取订单资金信息成功，订单状态为17,bpaidinfull=0");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create101_WaitPostPayOrder(userId, sellerId);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundInfo_012() {
		Logger.start(true, "获取订单资金信息成功，订单状态为17,bpaidinfull=1");
		try {
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfo order = c.create17_AcceptOrder(userId, sellerId);
			Thread.sleep(500l);
			Logger.comment("准备前置数据结束");
			getOrderFundInfoBean.setOrderId(order.getiOrderId());
			String param = FormatUtil.beanToHttpString(getOrderFundInfoBean,
					true, false);
			getOrderFundInfoCall.setData(param);
			getOrderFundInfoCall.callService();
//			Logger.comment("getOrderFundInfoCall send:"
//					+ getOrderFundInfoCall.getEntityString());
//			Logger.comment("getOrderFundInfoCall response:"
//					+ getOrderFundInfoCall.getReturnData());
			Logger.verifyEquals("200", getOrderFundInfoCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}